package com.study.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.study.view.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * author : xujianhong
 * date   : 2023/2/21
 * desc   :
 * version: 1.0
 */
public class MessageView extends FrameLayout {

    Context mContext;
    TextView textView;

    //开始位置
    Point initPosition;
    Point movePosition;

    //检测是否在当前的控件之内
    boolean isClick = false;
    //坐标原点画笔
    Paint paint;
    //原点的半径
    private float aRadio = 20;
    //存储路径
    Path path;
    boolean isOut = false;

    public MessageView(@NonNull Context context) {
        this(context,null);
    }

    public MessageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MessageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    private void init(){

        initPosition = new Point(100,100);
        movePosition = new Point(100,100);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        path = new Path();


        textView = new TextView(mContext);
        textView.setText("99+");
        textView.setTextColor(Color.WHITE);

        textView.setBackgroundResource(R.drawable.oval_bg_red);
        textView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT
                ,ViewGroup.LayoutParams.WRAP_CONTENT));
        this.addView(textView);
    }


    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        canvas.save();
        if(isClick){
            textView.setX(movePosition.x - textView.getWidth() / 2);
            textView.setY(movePosition.y - textView.getHeight() / 2);
            onDrawPath();

            if(!isOut){
                canvas.drawPath(path,paint);
                canvas.drawCircle(initPosition.x,initPosition.y, aRadio,paint);
                canvas.drawCircle(movePosition.x,movePosition.y, aRadio,paint);
            }
        }else{
            textView.setX(initPosition.x - textView.getWidth() / 2);
            textView.setY(initPosition.y - textView.getHeight() / 2);
        }

        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                Rect rect = new Rect();
                int [] textViewLocation = new int[2];
                textView.getLocationOnScreen(textViewLocation);
                rect.left = textViewLocation[0];
                rect.top = textViewLocation[1];
                rect.right = textViewLocation[0] + textView.getWidth();
                rect.bottom =  textViewLocation[1] + textView.getHeight();

                //是否在范围之内
                if(rect.contains((int) event.getRawX(),(int) event.getRawY())){
                    isClick = true;
                    movePosition.x = initPosition.x;
                    movePosition.y = initPosition.y;
                }

                break;
            case MotionEvent.ACTION_MOVE:
                if(isClick){
                    movePosition.x = (int) event.getX();
                    movePosition.y = (int) event.getY();
                }

                break;
            case MotionEvent.ACTION_UP:
                //手指抬起恢复到初始位置
                isClick = false;
                break;
        }
        postInvalidate();
        return true;
    }

    /**
     * 绘制连接桥
     * @param canvas
     */
    private void onDrawPath(){
        float widthX = movePosition.x - initPosition.x;
        float widthY = movePosition.y - initPosition.y;

        //固定点的圆随着拉伸的距离原来越小
        float widthC = (float) Math.sqrt(Math.pow(widthX, 2) + Math.pow(widthY, 2));
        aRadio = 20 - widthC/30;
        if(widthC >= 200){
            isOut = true;
        }else{
            isOut = false;
        }



        //获取正反玄值
        double atan = Math.atan(widthY/widthX);
        //对角三角形的两条边
        float offsetX = (float) (Math.sin(atan) * aRadio);
        float offsetY = (float) (Math.cos(atan)* aRadio);
        //获取到A点的坐标
        float PAx = initPosition.x - offsetX;
        float PAy = initPosition.y + offsetY;

        //获取到B点的坐标
        float PBx = movePosition.x - offsetX;
        float PBy = movePosition.y + offsetY;


        //获取到C点的坐标
        float PCx = movePosition.x + offsetX;
        float PCy = movePosition.y - offsetY;

        //获取到D点的坐标
        float PDx = initPosition.x + offsetX;
        float PDy = initPosition.y - offsetY;

        //获取贝塞尔曲线的控制点
        float conX = (initPosition.x + movePosition.x) / 2;
        float conY = (initPosition.y + movePosition.y) / 2;

        //先进行重置
        path.reset();
        path.moveTo(PAx,PAy);
        path.quadTo(conX,conY,PBx,PBy);
        //直线链接B点和C点
        path.lineTo(PCx,PCy);
        //贝塞尔曲线链接C点和D点
        path.quadTo(conX,conY,PDx,PDy);
        //从D点链接到A点
        path.lineTo(PAx,PAy);
    }
}
